﻿using System;
using DotGadu;

namespace GaduBot
{
    ///<summary>
    /// Klasa laczaca bota z biblioteka DotGadu
    ///</summary>
    /// <seealso cref="DotGadu.Gadu">
    /// Biblioteka DotGadu 
    /// </seealso>
    class GaduC
    {
        private static Logger log;
        private static GaduServer gaduServer;
        private static Gadu gadu;
        private static Responder resp;
        private static bool gaduInitialized = false;
        private static System.Windows.Forms.RichTextBox console;
        private static System.Windows.Forms.DataGridView tabela;
        private static System.Text.RegularExpressions.Regex RegexZapiszWiad = new System.Text.RegularExpressions.Regex("/$!*^/");
        private static GaduBot.Properties.Settings AppSets = new GaduBot.Properties.Settings();
        private static GaduBot.Properties.Resources AppRes = new GaduBot.Properties.Resources();

        #region Constructor and Destructor
        /// <summary>
        ///  Konstruktor klasy GacuC
        /// </summary>
        /// <param name="textbox"> Obiekt richTextBox, ktory posluzy za konsole </param>
        /// <param name="tabela"> Tabela do logowania danych </param>
        public GaduC(System.Windows.Forms.RichTextBox textbox,System.Windows.Forms.DataGridView tabela)
        {
            if (!GaduC.gaduInitialized)
            {
                this.Initialize(textbox, tabela);
            }
        }
        ~GaduC()
        {
            if (GaduC.gaduInitialized)
            {
                this.Destroy();
            }
        } 
        #endregion
        #region Inicjalizacja
        ///<summary>
        /// Funkcja laczace klienta i ustawiajaca go do pracy
        ///</summary>
        private void Initialize(System.Windows.Forms.RichTextBox textbox, System.Windows.Forms.DataGridView tabela)
        {
            GaduC.log = new Logger(textbox);
            GaduC.resp = new Responder();
            GaduC.console = textbox;
            GaduC.tabela = tabela;
            GaduC.gaduServer = new GaduServer(AppSets.GaduServerAddr, AppSets.GaduServerPort);
            GaduC.gadu = new Gadu();
            GaduC.gadu.Reciever.OnLoginOK += new OnLoginOKHandler(Receiver_OnOkLogin);
            GaduC.gadu.Reciever.OnLoginFailed += new OnLoginFailedHandler(Receiver_OnLoginFail);
            GaduC.gadu.Reciever.OnLoginNeedEmail += new OnLoginNeedEmailHandler(Receiver_OnLoginNeedEmail);
            GaduC.gadu.Reciever.OnRecieveMessage += new OnRecieveMessageHandler(Reciever_OnRecieveMessage);
            GaduC.gadu.Sender.OnPacketSended += new OnPacketSendedHandler(Receiver_OnSendPacket);
            GaduC.gadu.Connect(gaduServer);
            GaduC.gadu.Login(AppSets.GaduNumber, AppSets.GaduPass);
        }

        ///<summary>
        /// Funkcja, ktora wylogowuje klienta GG
        ///</summary>
        public void Destroy()
        {
            if (GaduC.gaduInitialized)
            {
                GaduC.gadu.Logout();
                GaduC.gadu.Disconnect();
            }
        } 
        #endregion
        #region Handlery dla DotGadu
        ///<summary>
        /// Uruchamiany przy odebraniu wiadomosci
        ///</summary>
        private static void Reciever_OnRecieveMessage(GaduPacketRecieveMessage msg)
        {
            string Response = GaduC.resp.Respond(msg.Message, msg.Sender);
            GaduC.gadu.sendMessage(msg.Sender, Response);
            log.Log(LogTypes.Info,"Odpowiedz: \"" + Response + "\" na wiadomosc: \"" + msg.Message + "\" od numeru: " + msg.Sender);
            tabela.Rows.Add(System.DateTime.Now.ToString(), msg.Sender, msg.Message, "IN");
            tabela.Rows.Add(System.DateTime.Now.ToString(), AppSets.GaduNumber, Response, "OUT");
        }   
        ///<summary>
        /// Uruchamiany przy wyslaniu pakiety danych
        ///</summary>
        private static void Receiver_OnSendPacket(IGaduPacket packet)
        {
            GaduC.log.Log(LogTypes.Debug,"Packet Sent");
        }
        
        ///<summary>
        /// Uruchamiany przy pomyslym logowaniu
        ///</summary>
        private static void Receiver_OnOkLogin()
        {
            GaduStatus status = new GaduStatus(GaduC.gadu.Sender);
            status.changeStatus(GaduPacketConstans.GG_STATUS_AVAIL_DESCR, "GGBot |ONLINE|");
            log.Log(LogTypes.Info,"Zalogowano pomyslnie.");
        }
        
        ///<summary>
        /// Uruchamiany przy bledzie logowania
        ///</summary>
        private static void Receiver_OnLoginFail()
        {
            log.Log(LogTypes.Fatal,"Blad logowania!");
        }

        ///<summary>
        /// Uruchamiany, gdy serwer informuje, 
        /// ze do zalogowania potrzebny jest email
        ///</summary>
        private static void Receiver_OnLoginNeedEmail()
        {
            log.Log(LogTypes.Error,"Trzeba uzupelnic email!");
        }
        #endregion
    }
}